package com.rc.saas.mini.controller.charge;


import com.rc.saas.mini.common.utils.HlhtQrCodeUtils;
import com.rc.saas.mini.model.elec.ElecGun;
import com.rc.saas.mini.model.hlht.HlhtOperator;
import com.rc.saas.mini.model.hlht.HlhtOperatorExample;
import com.rc.saas.mini.model.hlht.HlhtTenantOpen;
import com.rc.saas.mini.service.elec.ElecGunService;
import com.rc.saas.mini.service.hlht.HlhtOperatorService;
import com.rc.saas.mini.service.hlht.HlhtTenantOpenService;
import com.rc.saas.mini.vo.charge.QrCodeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 第三方运营商枪二维码解析
 * Created by Sven on 2021/08/06
 */
@Component
public class QrCodeParser {
    private String OPERATOR_66 = "359705330";//66快充
    private String OPERATOR_ZL = "MA5DM667X";//致联
    private String OPERATOR_XC = "313744932";//星充

    @Autowired
    private ElecGunService elecGunService;

    @Autowired
    private HlhtOperatorService hlhtOperatorService;
    @Autowired
    private HlhtTenantOpenService hlhtTenantOpenService;

    /**
     * 第三方运营商枪二维码解析
     * @return
     */
    public QrCodeVo parserOperatorQrCode(String qrCode) {
        String gunNo = null;
        String hlhtOperatorId = null;

        HlhtOperatorExample example = new HlhtOperatorExample();
        example.createCriteria().andStatusEqualTo("0");
        List<HlhtOperator> hlhtOperators = hlhtOperatorService.selectByExample(example);
        for (HlhtOperator hlhtOperator : hlhtOperators) { //遍历数据
            if (hlhtOperator.getHlhtOperatorId() != null) {
                hlhtOperatorId = hlhtOperator.getHlhtOperatorId();
                //枪二维码格式 0标准中电联格式 1非标准格式 默认0
                if (hlhtOperator.getQrcodeType().equals("0")) {
                    //析出标准的中电联二维码
                    gunNo = HlhtQrCodeUtils.getGunNoFromStdQrCode(hlhtOperator.getHlhtOperatorId(), qrCode);
                    if (gunNo != null) {
                        break;
                    }

                } else {
                    //66定制
                    if (OPERATOR_66.equals(hlhtOperatorId)) {
                        gunNo = HlhtQrCodeUtils.getGunNoFrom66(hlhtOperator.getQrcodePattern(), qrCode);
                        if (gunNo != null) {
                            break;
                        }
                    }
                    //致联定制
                    if (OPERATOR_ZL.equals(hlhtOperatorId)) {
                        gunNo = HlhtQrCodeUtils.getGunNoFromZL(qrCode);
                        if (gunNo != null) {
                            break;
                        }
                    }
                    //星充定制
                    if (OPERATOR_XC.equals(hlhtOperatorId)) {
                        gunNo = HlhtQrCodeUtils.getGunNoFromXC(qrCode);
                        if (gunNo != null) {
                            break;
                        }
                    }
                    //1其他非标准格式
                    gunNo = HlhtQrCodeUtils.getGunNoFromPattern(hlhtOperator.getQrcodePattern(), qrCode);
                    if (gunNo != null) {
                        break;
                    }
                }
            }
        }

        //智充根据二维码查找枪号
        if (gunNo == null) {
            ElecGun gun = elecGunService.selectByQrCode(qrCode);
            if (gun != null) {
                gunNo = gun.getGunNo();
                hlhtOperatorId = gun.getHlhtOperatorId() != null ? gun.getHlhtOperatorId() : null;
            }

        }
        //共创
        if (gunNo == null) {
            ElecGun gun = elecGunService.selectByGunNo(qrCode);
            if (gun != null) {
                gunNo = gun.getGunNo();
                hlhtOperatorId = gun.getHlhtOperatorId() != null ? gun.getHlhtOperatorId() : null;
            }

        }
        //星充第二套
        if (gunNo == null) {
            if (qrCode.length() == 8) {
                qrCode = "110000000000000" + qrCode + "000";
            }
            if (qrCode.length() == 10) {
                qrCode = "120000000000000" + qrCode.substring(0, qrCode.length() - 2) + "0" + qrCode.substring(qrCode.length() - 2);
            }
            ElecGun gun = elecGunService.selectByGunNo(qrCode);
            if (gun != null) {
                gunNo = gun.getGunNo();
                hlhtOperatorId = gun.getHlhtOperatorId() != null ? gun.getHlhtOperatorId() : null;
            }

        }

        QrCodeVo qrCodeVo = new QrCodeVo();
        qrCodeVo.setHlhtOperatorId(hlhtOperatorId);
        qrCodeVo.setGunNo(gunNo);

        return qrCodeVo;
    }


    /**
     * saas内互联运营商枪二维码解析
     * @return
     */
    public String parserSaasQrCode(String qrCode, String hlhtTenantCode) {
        String gunNo = null;
        List<HlhtTenantOpen> hlhtTenantOpens = hlhtTenantOpenService.listHlhtTenantOpenBySaas(hlhtTenantCode, "0");
        for (HlhtTenantOpen hlhtTenantOpen : hlhtTenantOpens) {
            gunNo = HlhtQrCodeUtils.getGunNoFromStdQrCode(hlhtTenantOpen.getTenantCode(), qrCode);
            if (gunNo != null) {
                break;
            }
        }
        return gunNo;
    }
}
